Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve memory consumption by cleaning up canceller function references when they are no longer needed #119

Merged
merged 1 commit into from
May 7, 2018

Conversation

clue
Copy link
Member

@clue clue commented May 6, 2018

Settling a pending Promise or Deferred means that its canceller function (if any) can no longer be invoked. By removing the reference to the canceller function in this case, we can ensure that an explicit reference back to the promise in the canceller now no longer causes a cyclic garbage reference in any exception trace as discussed in #46. This builds on top of the implementation approach in #117 and #118 and the ideas discussed in #46.

The gist here is that using a canceller that has an implicit reference to the promise (quite common due to closures bindings) now no longer causes a cyclic garbage reference in any exception trace and consumers of this package do not need to take special care of this.

Similar patches have been introduced with #117 and #118 to implement a similar logic for the internal cancellation callbacks and this PR takes advantage of this for its internal implementation.

Invoking the benchmarking example from #113 shows no effect, as reassigning variables is a rather cheap operation. In fact, explicitly adding some bogus references to this example shows a very significant performance and memory improvement! Initially this peaked somewhere around 8 MB on my system taking 7.3s. After applying this patch, this script reports a constant memory consumption of around 0.6 MB taking 1.9s.

This PR actually includes a test that shows how garbage memory references are no longer an issue in any supported PHP version and how explicitly using references no longer causes any such references on its own (this means that this requires no effort on the consumer side).

@clue clue added this to the v2.6.0 milestone May 6, 2018
@jsor jsor merged commit 308b916 into reactphp:2.x May 7, 2018
@clue clue deleted the canceller branch May 7, 2018 09:41
jsor added a commit to jsor-labs/pact that referenced this pull request May 16, 2018
This incorporates the work done by @clue in the following PR's for reactphp/promise:

* reactphp/promise#115
* reactphp/promise#116
* reactphp/promise#117
* reactphp/promise#118
* reactphp/promise#119

Co-authored-by: Christian Lück <[email protected]>
jsor added a commit to jsor-labs/pact that referenced this pull request May 16, 2018
This incorporates the work done by @clue in the following PR's for reactphp/promise:

* reactphp/promise#113
* reactphp/promise#115
* reactphp/promise#116
* reactphp/promise#117
* reactphp/promise#118
* reactphp/promise#119

Co-authored-by: Christian Lück <[email protected]>
clue added a commit to clue-labs/promise-timer that referenced this pull request Jun 11, 2018
clue added a commit to clue-labs/promise-timer that referenced this pull request Jun 11, 2018
clue added a commit to clue-labs/promise-timer that referenced this pull request Jun 11, 2018
clue added a commit to clue-labs/promise-timer that referenced this pull request Jun 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants